using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
using System;
using UnityEngine.UI;
using System.Linq;

namespace Super
{
	public static class TableUtil
	{
		// 参数之间用逗号分隔	案例“1,2,3”
		private static string[] SplitStr(string strArgs, ParseStrArgSetting setting = null)
		{
			setting ??= new ParseStrArgSetting();
			strArgs = strArgs.Trim();
			if (setting.isClearParenthesis)
			{
				strArgs = strArgs.Trim('(', ')', '（', '）');
			}
			if (setting.isClearSquareBrackets)
			{
				strArgs = strArgs.Trim('[', ']', '【', '】');
			}
			var splitArgs = strArgs.Split(',', '，');

			return splitArgs;
		}

		// 数组用下划线分隔，参数之间用逗号分隔		案例“(1,2,3)_(4,5,6)”
		private static string[] SplitStrArr(string strArgsArr, ParseStrArgSetting setting = null)
		{
			setting ??= new ParseStrArgSetting();
			strArgsArr = strArgsArr.Trim();
			if (setting.isClearParenthesis)
			{
				strArgsArr = strArgsArr.Trim('(', ')', '（', '）');
			}
			if (setting.isClearSquareBrackets)
			{
				strArgsArr = strArgsArr.Trim('[', ']', '【', '】');
			}
			var splitArgsArr = strArgsArr.Split('_');

			for (int i = 0; i < splitArgsArr.Length; i++)
			{
				if (setting.isClearParenthesis)
				{
					splitArgsArr[i] = splitArgsArr[i].Trim('(', ')', '（', '）');
				}
				if (setting.isClearSquareBrackets)
				{
					splitArgsArr[i] = splitArgsArr[i].Trim('[', ']', '【', '】');
				}
			}

			return splitArgsArr;
		}

		/// <summary>
		/// 解析字符串参数，返回List<T>，stringArgs案例“1,2,3”
		/// </summary>
		/// <param name="strArgs"></param>
		/// <param name="setting"></param>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static List<T> ParseStrToList<T>(string strArgs, ParseStrArgSetting setting = null)
		{
			if (string.IsNullOrEmpty(strArgs) || strArgs.ToLower() == "null")
				return new List<T>();

			setting ??= new ParseStrArgSetting();
			var splitArgs = SplitStr(strArgs, setting);
			List<T> result = new List<T>();
			foreach (var arg in splitArgs)
			{
				object value = null;
				if (typeof(T).IsEnum)
				{
					value = System.Enum.Parse(typeof(T), arg, true);
				}
				else
				{
					value = Convert.ChangeType(arg, typeof(T));	
				}
				result.Add((T)value);
			}
			return result;
		}

		/// <summary>
		/// 解析字符串参数数组，返回List<List<T>>，stringArr案例“(1,2,3)_(4,5,6)”
		/// </summary>
		/// <param name="strArgsArr"></param>
		/// <param name="setting"></param>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static List<List<T>> ParseStrToListList<T>(string strArgsArr, ParseStrArgSetting setting = null)
		{
			if (string.IsNullOrEmpty(strArgsArr) || strArgsArr.ToLower() == "null")
				return new List<List<T>>();

			setting ??= new ParseStrArgSetting();
			var splitStrArgsArr = SplitStrArr(strArgsArr, setting);
			List<List<T>> result = new List<List<T>>();
			foreach (var strArgs in splitStrArgsArr)
			{
				var value = ParseStrToList<T>(strArgs, setting);
				result.Add(value);
			}
			return result;
		}

		/// <summary>
		/// 解析字符串参数，返回T，stringArgs	案例“1,2，Grass” 返回MapTerrainNode
		/// </summary>
		/// <param name="strArgs"></param>
		/// <param name="setting"></param>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static T ParseStrToClass<T>(string strArgs, ParseStrArgSetting setting = null) where T : class, ISetValue
		{
			if (string.IsNullOrEmpty(strArgs) || strArgs.ToLower() == "null")
				return null;

			setting ??= new ParseStrArgSetting();
			var splitArgs = SplitStr(strArgs, setting);
			T result = (T)Activator.CreateInstance(typeof(T), null);
			result.SetValue(splitArgs);
			return result;
		}

		/// <summary>
		/// 解析字符串参数数组，返回List<T>，stringArgsArr案例“(1,2,Grass)_(4,5,Grass)” 返回List<MapTerrainNode>
		/// </summary>
		/// <param name="strArgsArr"></param>
		/// <param name="setting"></param>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static List<T> ParseStrToClassList<T>(string strArgsArr, ParseStrArgSetting setting = null) where T : class, ISetValue
		{
			if (string.IsNullOrEmpty(strArgsArr) || strArgsArr.ToLower() == "null")
				return new List<T>();

			setting ??= new ParseStrArgSetting();
			var splitStrArgsArr = SplitStrArr(strArgsArr, setting);
			List<T> result = new List<T>();
			foreach (var strArgs in splitStrArgsArr)
			{
				var value = ParseStrToClass<T>(strArgs, setting);
				result.Add(value);
			}
			return result;
		}

	}
}


